package com.vdong.restful.util;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vdong.data.model.Template;
import com.vdong.data.model.TemplateCarryMode;
import com.vdong.data.request.TemplateArea;
import com.vdong.restful.api.response.TemplateAreaResponse;
import com.vdong.restful.api.response.TemplateCarryModeResponse;
import com.vdong.restful.api.response.TemplateListResponse;
import com.vdong.restful.api.response.TemplateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 模板dbTemplate 数据转换 response 格式 1对多运费 1多对地区
 *
 * @author liangwei
 */
@Component
public class TemplateToTemplateResponseUtil {
    @Qualifier("ObjectMapper")
    @Autowired
    ObjectMapper objectMapper;

    /**
     * 模板转换实现
     * @param template
     * @return
     */
    public TemplateResponse toResponse(Template template) {

        TemplateCarryModeResponse templateCarryModeResponsebean;

        List<TemplateCarryModeResponse> templateCarryModeResponses = new ArrayList<TemplateCarryModeResponse>();
        List<TemplateArea> templateAreals;
        List<TemplateAreaResponse> templateAreaResponses;
        TemplateAreaResponse templateAreaResponse;
        JavaType javatype = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, TemplateArea.class);
        for (TemplateCarryMode templateCarryMode : template.getTemplateCarryMode()) {
            templateCarryModeResponsebean = new TemplateCarryModeResponse();
            templateCarryModeResponsebean.setActualAmount(templateCarryMode.getActualAmount());
            templateCarryModeResponsebean.setMinAmount(templateCarryMode.getMinAmount());
            templateCarryModeResponsebean.setMode(templateCarryMode.getMode());
            templateCarryModeResponsebean.setShipperId(templateCarryMode.getShipperId());
            try {
                templateAreals = objectMapper.readValue(templateCarryMode.getAreas(), javatype);
            } catch (Exception e) {
                return null;
            }
            templateAreaResponses = new ArrayList<TemplateAreaResponse>();

            for (TemplateArea templateArea : templateAreals) {

                templateAreaResponse = new TemplateAreaResponse();
                templateAreaResponse.setCitiesName(templateArea.getCitiesName());
                templateAreaResponse.setProincesName(templateArea.getProvincesName());

                templateAreaResponses.add(templateAreaResponse);

            }
            templateCarryModeResponsebean.setTemplateAreaResponse(templateAreaResponses);
            templateCarryModeResponses.add(templateCarryModeResponsebean);
        }

        TemplateResponse templateResponse = new TemplateResponse();
        templateResponse.setCreateTime(template.getCreateTime());
        templateResponse.setName(template.getName());

        templateResponse.setState(template.getState());
        templateResponse.setUserId(template.getUserId());
        templateResponse.setId(template.getId());
        templateResponse.setTemplateCarryModeResponse(templateCarryModeResponses);

        return templateResponse;
    }

    public TemplateResponse templateCarryModeToTemplateResponse(List<TemplateCarryMode> templateCarryModes) {

        TemplateCarryModeResponse templateCarryModeResponsebean;

        List<TemplateCarryModeResponse> templateCarryModeResponses = new ArrayList<TemplateCarryModeResponse>();
        List<TemplateArea> templateAreals;
        List<TemplateAreaResponse> templateAreaResponses;
        TemplateAreaResponse templateAreaResponse;
        JavaType javatype = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, TemplateArea.class);
        for (TemplateCarryMode templateCarryMode : templateCarryModes) {
            templateCarryModeResponsebean = new TemplateCarryModeResponse();
            templateCarryModeResponsebean.setActualAmount(templateCarryMode.getActualAmount());
            templateCarryModeResponsebean.setMinAmount(templateCarryMode.getMinAmount());
            templateCarryModeResponsebean.setMode(templateCarryMode.getMode());
            templateCarryModeResponsebean.setShipperId(templateCarryMode.getShipperId());
            try {
                templateAreals = objectMapper.readValue(templateCarryMode.getAreas(), javatype);
            } catch (Exception e) {
                return null;
            }
            templateAreaResponses = new ArrayList<TemplateAreaResponse>();

            for (TemplateArea templateArea : templateAreals) {

                templateAreaResponse = new TemplateAreaResponse();
                templateAreaResponse.setCitiesName(templateArea.getCitiesName());
                templateAreaResponse.setProincesName(templateArea.getProvincesName());

                templateAreaResponses.add(templateAreaResponse);

            }
            templateCarryModeResponsebean.setTemplateAreaResponse(templateAreaResponses);
            templateCarryModeResponses.add(templateCarryModeResponsebean);
        }

        TemplateResponse templateResponse = new TemplateResponse();
        Template template = templateCarryModes.get(0).getTemplate();
        templateResponse.setCreateTime(template.getCreateTime());
        templateResponse.setName(template.getName());

        templateResponse.setState(template.getState());
        templateResponse.setUserId(template.getUserId());
        templateResponse.setId(template.getId());
        templateResponse.setTemplateCarryModeResponse(templateCarryModeResponses);

        return templateResponse;
    }

    /**
     * 单独 template实体转换
     */
    public static TemplateListResponse templateToResponse(Template template) {
        TemplateListResponse templateResponse = new TemplateListResponse();
        templateResponse.setCreateTime(template.getCreateTime());
        templateResponse.setName(template.getName());

        templateResponse.setState(template.getState());
        templateResponse.setUserId(template.getUserId());
        templateResponse.setId(template.getId());
        return templateResponse;

    }

    /**
     * 集合模板转换
     *
     * @param templates
     * @return
     */
    public static List<TemplateListResponse> toListResponse(List<Template> templates) {
        List<TemplateListResponse> templateResponses = new ArrayList<TemplateListResponse>();
        for (Template template : templates) {
            templateResponses.add(templateToResponse(template));
        }

        return templateResponses;
    }

}